#!/bin/bash -eu
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################

path=$1
function=$2
fuzzer=$3
tags="-tags gofuzz"

# Get absolute path.
abs_file_dir=$(go list $tags -f {{.Dir}} $path)

# TODO(adamkorcz): Get rid of "-r" flag here.
export fuzzer_filename=$(grep -r -l --include='*.go' -s "$function" "${abs_file_dir}")

# Import build_native_go_fuzzer_legacy
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/go_utils.sh"

# Test if file contains a line with "func $function" and "testing.F".
if [ $(grep -r "func $function" $fuzzer_filename | grep "testing.F" | wc -l) -eq 1 ]
then
	build_native_go_fuzzer_legacy $fuzzer $function $abs_file_dir $path
else
	echo "Could not find the function: func ${function}(f *testing.F)"
fi
